package com.generator.view.util;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串辅助工具类
 *
 * @author jaybril-pro
 * @date 2019-03-15
 */
public class StringUtils extends org.springframework.util.StringUtils {

    public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String valueOf(Object str) {
        if (str == null) {
            return "";
        }
        if (str instanceof Date) {
            return SDF.format((Date) str);
        }
        return str.toString();
    }

    public static List<String> tokenizer(Object str) {
        return tokenizer(str, ", \t\n\r\f");
    }

    public static List<String> tokenizer(Object str, String split) {
        if (str == null) {
            return new LinkedList();
        }
        String string = str.toString();
        StringTokenizer st = new StringTokenizer(string, split, false);
        List<String> valueList = new ArrayList();
        while (st.hasMoreElements()) {
            valueList.add(st.nextToken());
        }
        return valueList;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    /**
     * 判断多个字符串是否为空
     *
     * @param isAnd 是否同时为空成立
     * @param args  字符串数组
     * @return Boolean
     */
    public static boolean isEmpty(boolean isAnd, String... args) {
        for (String string : args) {
            boolean isTrue = isEmpty(string);
            if (isAnd) {
                //只要有一个不为空，就返回非空
                if (!isTrue) {
                    return false;
                }
            } else {
                //只要有一个为空，就返回空
                if (isTrue) {
                    return true;
                }
            }
        }
        return isAnd;
    }

    public static void main(String[] args) {
        System.out.println(StringUtils.isEmpty(true, "", "ss"));
    }

    public static boolean isNumber(String str) {
        if (str == null) {
            return false;
        }
        char[] cs = str.toCharArray();
        for (char c : cs) {
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }

    /**
     * 过滤特殊字符
     *
     * @param string 要过滤的字符串
     * @return String 过滤后的字符串
     */
    public static String filterString(String string) {
        String regEx = "[`~!@#$%^&*()+=【】{}—+|@#￥%……&*（）”“。，、？－:<>；：.‘’\"?！]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(string);
        return m.replaceAll("").trim();
    }

    /**
     * 过滤表情符号
     *
     * @param source
     * @return String
     */
    public static String filterEmoji(String source) {
        if (StringUtils.isEmpty(source)) {
            return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
        } else {
            return source;
        }
    }

    /**
     * 计算字符串长度（包括中文）
     *
     * @param value
     * @return int
     */
    public static int length(String value) {
        int valueLength = 0;
        String chinese = "[\u0391-\uFFE5]";
        /* 获取字段值的长度，如果含中文字符，则每个中文字符长度为2，否则为1 */
        for (int i = 0; i < value.length(); i++) {
            /* 获取一个字符 */
            String temp = value.substring(i, i + 1);
            /* 判断是否为中文字符 */
            if (temp.matches(chinese)) {
                /* 中文字符长度为2 */
                valueLength += 2;
            } else {
                /* 其他字符长度为1 */
                valueLength += 1;
            }
        }
        return valueLength;
    }

    /**
     * 生成唯一随机数
     *
     * @param length 随机数的长度
     * @return String
     */
    public static String getRandom(Integer length) {
        String string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random ran = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
//			生成一个随机的int值，该值介于[0,62)的区间
            int num = ran.nextInt(62);
            sb.append(string.charAt(num));
        }
        return sb.toString();
    }

    /**
     * 转换为驼峰
     *
     * @param underscoreName 下划线字符串
     * @return String 转换后的内容
     */
    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (underscoreName != null && underscoreName.length() > 0) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }

    /**
     * 字符串转int 数组
     *
     * @param arr
     * @return
     */
    public static int[] stringToIntArr(String[] arr) {
        int[] ints = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ints[i] = Integer.valueOf(arr[i]);
        }
        return ints;
    }
    public static Long[] stringToLongArr(String[] arr) {
        Long[] ints = new Long[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ints[i] = Long.valueOf(arr[i]);
        }
        return ints;
    }

    /**
     * 字符串数组转Long数据
     *
     * @param stringArray 字符串数组
     * @return Long[] 数组
     */
    public static Long[] stringToLong(String[] stringArray) throws Exception {
        if (stringArray == null || stringArray.length < 1) {
            throw new Exception("数组内容为空");
        }
        Long[] longArray = new Long[stringArray.length];
        for (int i = 0; i < longArray.length; i++) {
            longArray[i] = Long.valueOf(stringArray[i]);
        }
        return longArray;
    }


}
